<?php

/*

Desktopd Colors
Copyright (C) 2015  Desktopd Developer(s)

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU Affero General Public License for more details.

You should have received a copy of the GNU Affero General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.

https://www.gnu.org/licenses/agpl.html

*/


namespace Desktopd\Colors;


class sRGBPlot {
    protected $colors = array();
    
    protected $path_RG = false;
    protected $path_RB = false;
    protected $path_GB = false;
    
    protected $R_min;
    protected $R_max;
    protected $G_min;
    protected $G_max;
    protected $B_min;
    protected $B_max;
    
    public function __construct() {
        
    }
    
    public function addColor(sRGB $color) {
        $this->colors[] = $color;
        
        $R = $color->getR();
        $G = $color->getG();
        $B = $color->getB();
        
        $this->R_min = isset($this->R_min) ? min($this->R_min, $R) : $R;
        $this->R_max = isset($this->R_max) ? max($this->R_max, $R) : $R;
        
        $this->G_min = isset($this->G_min) ? min($this->G_min, $G) : $G;
        $this->G_max = isset($this->G_max) ? max($this->G_max, $G) : $G;
        
        $this->B_min = isset($this->B_min) ? min($this->B_min, $B) : $B;
        $this->B_max = isset($this->B_max) ? max($this->B_max, $B) : $B;
        
        
    }
    
    public function saveRG($path) {
        $this->path_RG = $path;
    }
    
    public function saveRB($path) {
        $this->path_RB = $path;
    }
    
    public function saveGB($path) {
        $this->path_GB = $path;
    }
    
    
    public function render($minWidth = 4096, $minHeight = 2560) {
        if ($this->path_RG !== false) {
            $RG_plot = new Plot(
                $minWidth,
                $minHeight,
                array($this->R_max, $this->R_min),
                array($this->G_max, $this->G_min)
            );
        }
        
        if ($this->path_RB !== false) {
            $RB_plot = new Plot(
                $minWidth,
                $minHeight,
                array($this->R_max, $this->R_min),
                array($this->B_max, $this->B_min)
            );
        }
        
        if ($this->path_GB !== false) {
            $GB_plot = new Plot(
                $minWidth,
                $minHeight,
                array($this->G_max, $this->G_min),
                array($this->B_max, $this->B_min)
            );
        }
        
        $whitepoint = (new sRGB(1, 1, 1))->toXYZ();
        
        foreach ($this->colors as $color) {
            $R = $color->getR();
            $G = $color->getG();
            $B = $color->getB();
            
            if ($this->path_RG !== false) {
                $RG_plot->plot($R, $G, $color);
            }
            
            if ($this->path_RB !== false) {
                $RB_plot->plot($R, $B, $color);
            }
            
            if ($this->path_GB !== false) {
                $GB_plot->plot($G, $B, $color);
            }
        }
        
        if ($this->path_RG !== false) {
            $RG_plot->save($this->path_RG);
        }
        
        if ($this->path_RB !== false) {
            $RB_plot->save($this->path_RB);
        }
        
        if ($this->path_GB !== false) {
            $GB_plot->save($this->path_GB);
        }
        
        return true;
    }
    
    public function __invoke() {
        return $this->render();
    }
    
    public function __toString() {
        return "R: [{$this->R_min}, {$this->R_max}], G: [{$this->G_min}, {$this->G_max}], B: [{$this->B_min}, {$this->B_max}]";
    }
}


// vim: ts=4 et ai

